<?php
namespace app\index\controller;

use app\index\model\JoinModel;
use app\index\model\UtilModel;
use think\Controller;
use think\Request;
use think\Loader;
use think\Db;

class Webmeeting extends Controller
{

    /**
     * 医生登陆
     * @return mixed
     */
    public function Xelodadoctor(){
        if(Request::instance()->isPost()){
            $data['user_name'] = input('user_name','','trim');
            $data['doc_hospital'] = input(  'doc_hospital','','trim');
            $data['mobile'] = input('mobile','','trim');
            $validate = Loader::validate('Login');
            if (!$validate->scene('doctor')->check($data)) {
                return $this->error($validate->getError());
            }
            $joinModel = new JoinModel();
            $data['join_type'] = 2;
            $data['add_time'] = time();
            $res = $joinModel->joinMeetingInfo($data);
            if($res){
                return $this->success('登陆成功','http://www.bizconfstreaming.cn/webcast/wljd.html?nickName='.$data['user_name']);
            }else{
                return $this->error('登陆失败');
            }

        }else{
            return $this->fetch('index/doctor_login');
        }
    }
    public function export(){
        if(Request::instance()->isPost()){
            $data['start'] = $start = input('start_time','','trim');
            $data['end'] = $end = input('end_time','','trim');
            $data['type'] = $type = input('join_type',0,'intval');

            $validate = Loader::validate('Export');
            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }
            $result = Db::name('join')
                ->alias('j')
                ->join('user u','u.user_id = j.user_id','left')
                ->where('j.user_id','>',0)
                ->field('j.*,u.user_name')
                ->where('j.add_time','>=',strtotime($start))
                ->where('j.add_time','<=',strtotime($end))
                ->where('join_type',$type)
                ->select();
            if(empty($result)){
                return $this->error('暂无数据');
            }
            foreach ($result as $key=>$r){
                $result[$key]['province_id'] = Db::name('region')->where('region_id',$r['province_id'])->value('region_name');
                $result[$key]['city_id'] = Db::name('region')->where('region_id',$r['city_id'])->value('region_name');
                $result[$key]['hospital_id'] = Db::name('hospital')->where('hospital_id',$r['hospital_id'])->value('hospital_name');
                $result[$key]['subject_id'] = Db::name('subject')->where('subject_id',$r['subject_id'])->value('subject_name');
            }

            return  $this->batchJoinInfo($result);
        }else{
            return $this->fetch('index/export');
        }

    }
    public function getUserData(){
            $data['start'] = $start = input('post.start_time','','trim');
            $data['end'] = $end = input('post.end_time','','trim');
            $data['type'] = $type = input('post.join_type','','trim');
            $validate = Loader::validate('User');
            if (!$validate->check($data)) {
                return restError($validate->getError());
            }
            $result = Db::name('join')
                ->alias('j')
                ->join('user u','u.user_id = j.user_id','left')
                ->where('j.user_id','>',0)
                ->field('j.province_id,j.city_id,j.hospital_id,j.subject_id,j.count,FROM_UNIXTIME(j.add_time,"%Y-%m-%d %H:%i:%s") as add_time,u.user_name,u.user_email,u.user_tel')
                ->where('j.add_time','>=',strtotime($start))
                ->where('j.add_time','<=',strtotime($end))
                ->where('join_type',$type)
                ->select();
            if(empty($result)){
                return restSuccess('处理成功',array());
            }
            foreach ($result as $key=>$r){
                $result[$key]['province'] = Db::name('region')->where('region_id',$r['province_id'])->value('region_name');
                $result[$key]['city'] = Db::name('region')->where('region_id',$r['city_id'])->value('region_name');
                $result[$key]['hospital'] = Db::name('hospital')->where('hospital_id',$r['hospital_id'])->value('hospital_name');
                $result[$key]['subject'] = Db::name('subject')->where('subject_id',$r['subject_id'])->value('subject_name');
            }
            return restSuccess('处理成功',$result);

    }
    /**
     * 导出信息
     */
    public function batchJoinInfo($result){
        import('util.PHPExcel.Classes.PHPExcle');
        import('util.PHPExcel.Classes.PHPExcel.IOFactory');
        $export_excel_name = '参会信息';
        $objPHPExcel = new \PHPExcel();

        //设置属性
        $objPHPExcel->getProperties()->setCreator("码魔")
            ->setLastModifiedBy("码魔")
            ->setTitle("参会信息采集报表")
            ->setSubject("参会信息采集报表")
            ->setDescription("参会信息采集报表")
            ->setKeywords("excel")
            ->setCategory("result file");
        // set width
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);

        // 设置行高度
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);
        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);

        // 字体和样式
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('A1:S1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A1:S1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1:S1')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        // 设置水平居中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


        //设置导航
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '姓名')
            ->setCellValue('B1', '省份')
            ->setCellValue('C1', '城市')
            ->setCellValue('D1', '医院')
            ->setCellValue('E1', '科室')
            ->setCellValue('F1', '参会人数')
            ->setCellValue('G1', '参会时间');

        foreach($result as $k => $v){
            $num=$k+2;
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$num, $v['user_name'])
                ->setCellValue('B'.$num, $v['province_id'])
                ->setCellValue('C'.$num, $v['city_id'])
                ->setCellValue('D'.$num, $v['hospital_id'])
                ->setCellValue('E'.$num, $v['subject_id'])
                ->setCellValue('F'.$num, $v['count'])
                ->setCellValue('G'.$num, date('Y-m-d H:i:s',$v['add_time']));
        }
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$export_excel_name.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
        return 1;
    }

}
